'''
案例9.11：使用requests和re库爬取猫眼电影100榜单
'''
#导入requests和re库
import requests,re

#定义函数基于requests库获取网页源代码
def getHTML(url):
    try:
        headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',                 	'Cookie':'__mta=149216905.1597197668286.1597197816775.1597204560451.4; uuid_n_v=v1; uuid=AF846040DC3F11EA9BFE43BA456BAEDEA0632615762B4C85B3CCB9191253B9BC; _csrf=33b46c094a00117b21f455bbc90e8e4f3cd3f9a11afc371b47818cc016748653; _lxsdk_cuid=173e0664a4ec8-0867d95a81fd6c-37664109-280000-173e0664a4fc8; _lxsdk=AF846040DC3F11EA9BFE43BA456BAEDEA0632615762B4C85B3CCB9191253B9BC; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1597197667; mojo-uuid=314539836e7f11a182f6b11ee6221d8c; mojo-session-id={"id":"a5e9eb025b8323be5867fec83e9e5980","time":1597204560286}; mojo-trace-id=1; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1597204560; _lxsdk_s=173e0cf79bc-5cd-8af-a2e%7C%7C2'
                 }
        r = requests.get(url,headers=headers)
        r.raise_for_status()
        html = r.text
    except:
        print("error")
    return html

#定义函数基于正则表达式获取目标内容,参数html为网页源代码，pattern为匹配表达式
def getMatchText(html,pattern=None):
    p = re.compile(pattern)
    return p.findall(html)

#主函数，定义目标网页地址url，匹配表达式获取结果
if __name__=='__main__':
    url='https://maoyan.com/board/4'
    #获得猫眼榜单页面源代码
    html = getHTML(url)
    #去除代码中的换行符
    html = re.sub(r'\n ','',html)
    #去除代码中的空格符
    html = re.sub(r' ','',html)
    #获取榜单电影名的正则表达式
    p_name='class="name"><a.*title="(.*?)"'
    # 获取榜单电影主演的正则表达式
    p_star=r'class="star">(.*?)</p>'
    # 获取榜单电影上映时间的正则表达式
    p_releasetime='class="releasetime">(.*?)</p>'
    # 获取榜单电影评分的正则表达式
    p_score='.*?integer">(.*?)</i>.*?fraction">(.*?)</i>'
    p_movie=[p_name,p_star,p_releasetime,p_score]
    for item in p_movie:
        print(getMatchText(html,pattern=item))